 
import { ABaseDecision } from "GameLoop/DecisionMaking/BaseDecision";
import { MainStrategy } from "GameLoop/WorldState/EntityManager/CreateStrategy/MainStrategy";
import { InnerMineEntity } from "GameLoop/WorldState/EntityManager/Entitys";
import { MineEntity } from "GameLoop/WorldState/EntityManager/Entitys/room/mine/MineEntity";
import { priority } from "utils/constants/definetypes";
import { Roles } from "utils/creep/setups";
import { generateUniqueId } from "utils/utils";

/**
 * 升级时的。放置自定义site
 */
export class PlaceMinePlanDecision< T extends "room"="room"> extends ABaseDecision<T>
{
   
    public isExpired(event: AddRoomEvent, region: string): boolean 
    {
        const room = Game.rooms[event.region] as Required<Room>;
        if(room.findTaskById(`place_plan_${event.region}`,"room"))
        {
            return false;
        }
        return true;
    }
    public makeTask(event: AddRoomEvent, region: string): IDecisionCode<T>|false
    {
        const room = Game.rooms[event.region] as Required<Room>;
        // 3级时放路
        if(room.controller.level >=3)
        {
            const plans:Record<string,IPlanItem[]>={};
            // 外矿内矿放路
            const innerMine = room.getEntitys("InnerMineEntity");
            for(const e of innerMine)
            {
                this.getRoadPlan(e,plans);
            }
            
            const outMine = room.getEntitys("OutMineEntity");
            for(const e of outMine)
            {
                if(e.getNodeMemory().state)
                {
                    continue;
                }
                if(!plans[ e.WorkPosition.roomName])
                {
                    plans[ e.WorkPosition.roomName]=[];
                }
                plans[ e.WorkPosition.roomName].splice(0,0,{
                    pos:{x:e.WorkPosition.x,y:e.WorkPosition.y},
                    type:STRUCTURE_CONTAINER
                })
                this.getRoadPlan(e,plans);
               
            }
             
            const code:IDecisionCode<T>={
                region:event.region,
                task:[],
                isunique:false,
            }
            for(const k in plans)
            {
                const task :ITaskDeclare[T]={
                    id:`place_plan_${k}`,
                    priority:priority.E,
                    ttl:5000,
                    type:"room",
                    data:{
                        type:"placePlan",
                        arg:{
                            roomName:k,
                            plan:plans[k]
                        },
                    },
                    shard:0,
                    
                }
                code.task.push(task);
            }
            
            return code
        }
        return false;
        // return code
    }
    
    private getRoadPlan(mine:MineEntity,record:Record<string,IPlanItem[]>)
    {
        const paths = mine.getPath();
 
        for(const e of paths)
        {
            if(record[e.roomName])
            {
                record[e.roomName].push({pos:{x:e.x,y:e.y},type:STRUCTURE_ROAD});
            }
            else
            {
                record[e.roomName]= [{pos:{x:e.x,y:e.y},type:STRUCTURE_ROAD}];
            }
        }
        return record;
    }
}